package com.tigerhhzz.service;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
public class SmsSenderService {

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void sendSmsAsync(String phoneNumber) {
        // 生成 UUID 作为唯一 ID
        String uniqueId = UUID.randomUUID().toString();
        // 先记录发送任务到数据库，初始状态为未发送
        jdbcTemplate.update("INSERT INTO sms_log (phone_number, message_content, send_status, unique_id) VALUES (?,?, 0,?)", phoneNumber, null, uniqueId);
        String message = phoneNumber + "," + uniqueId;

        Map<String,String> map=new HashMap();
        map.put("phoneNumber", phoneNumber);
        map.put("uniqueId", uniqueId);


        amqpTemplate.convertAndSend("smsExchange", "smsRoutingKey", map);
    }
}